<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class User_lib {

    function _formatXML($xml_file) {
        $xml = simplexml_load_file($xml_file);
        $controllers = array();

        foreach($xml->controllers as $controller) {

            $controller = $controller->controller;
            foreach ($controller as $ctl){
                $controller_array['name'] = (string) $ctl->attributes()->name;
                $controller_array['title'] = (string) $ctl->attributes()->title;
                // Store methods from the controller
                $controller_array['methods'] = array();
                if($ctl->method) {
                    // Loop through to save methods
                    foreach($ctl->method as $method) {
                        $controller_array['methods'][(string) $method->name] = (string) $method->description;
                    }
                }

                // Save it all to one variable
                $controllers[$controller_array['name']] = $controller_array;
            }
        }

        return array(
            'version' 		=> 	(float) $xml->attributes()->version,
            'type' 			=> 	(string) $xml->attributes()->type,
            'description' 	=> 	(string) $xml->description->{'vi'},
            'icon' 			=> 	(string) $xml->icon,
            'required'		=>	$xml->required == 1,
            'is_frontend'	=>	$xml->is_frontend == 1,
            'is_backend'	=>	$xml->is_backend == 1,
            'is_backend_sidebar'	 =>	$xml->is_backend_sidebar == 1,
            'controllers'	=>	$controllers
        );
    }

    function prepareJsonTree($arrayPath=array()) {
        $xml_file = '';
        if(file_exists($xml_file = APPPATH.'controllers/details.xml'))
        {
            $xml_file = APPPATH.'controllers/details.xml';
        }
        else {
            show_404("Missing file details.xml.");
        }
        $content = "";

        // Root note
        $content = $content . "[{\"text\":\"Modules\",\"id\":\"1\",\"depth\":\"0\",\"leaf\":false,\"checked\":\"true\", \"children\":[\n";

        // Get modules
        $i = 0;
        $array = $this->_formatXML($xml_file);
        $controllers = $array['controllers'];
        foreach ($controllers as $controller ) {
            if ($i > 0)
                $content = $content . ",";
            if (isset($controller['title']) && isset($controller['name']) ) {
                $content = $content . "{\"text\":\"" . $controller['title'] . ' (' . $controller['name'] . ')'  . "\",\"id\":\"" . $controller['name'] . "\",\"depth\":\"0\",\"leaf\":false,\"checked\":\"true\"";
                $content = $content.", \"children\":[\n";	//start controller children
            }
            $j = 0;
            foreach($controller['methods'] as $method=>$description) {
                if ($j > 0)
                    $content = $content . ",";
                $id = strtolower($controller['name']."/".$method);
                $checkedStatus = ",\"checked\":\"true\"";
                $content = $content."{\"text\":\"".$method."\",\"id\":\"".$id."\",\"depth\":\"0\",";
                if(isset($description)) {
                    $content = $content."\"rel\":\"( ".$description." )\",";
                }
                $content .= "\"leaf\":true".$checkedStatus."}";
                $j++;
            }

            $content = $content . "]}\n";
            $i++;
        }
        $content = $content . "]}]\n"; //End root note
        file_put_contents(FCPATH . 'assets/json/treeView.json',$content);
    }

}